package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.project.domain.ApproveInfo;
import net.hn.hnms.biz.project.domain.ProjectFile;
import net.hn.hnms.biz.project.domain.StandingBookManage;
import net.hn.hnms.biz.project.mapper.ApprovalPersonMapper;
import net.hn.hnms.biz.project.mapper.ApproveInfoMapper;
import net.hn.hnms.biz.project.mapper.ProjectFileMapper;
import net.hn.hnms.biz.project.mapper.StandingBookManageMapper;
import net.hn.hnms.biz.project.request.StandingBookManageReq;
import net.hn.hnms.biz.project.request.StandingFileReq;
import net.hn.hnms.biz.project.service.StandingBookManageService;
import net.hn.hnms.biz.project.vo.StandingBookManageVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.utils.LoginHelper;
import net.hn.hnms.sys.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author jayx
 * @description 针对表【standing_book_manage】的数据库操作Service实现
 * @createDate 2023-03-13 16:44:03
 */
@Service
public class StandingBookManageServiceImpl extends ServiceImpl<StandingBookManageMapper, StandingBookManage>
        implements StandingBookManageService {

    @Autowired
    private StandingBookManageMapper standingBookManageMapper;

    @Autowired
    private ProjectFileMapper projectFileMapper;

    @Autowired
    private ApprovalPersonMapper approvalPersonMapper;

    @Autowired
    private ApproveInfoMapper approveInfoMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Override
    @Transactional
    public int insert(StandingBookManageReq standingBookManageReq) {
        StandingBookManage insertObj = new StandingBookManage();
        BeanUtil.copyProperties(standingBookManageReq, insertObj);
        int falg = standingBookManageMapper.insert(insertObj);

        String id = insertObj.getId();

        ApproveInfo flowInfo = new ApproveInfo();
        flowInfo.setStandingBookManageId(id);
        flowInfo.setApproveUsername(insertObj.getBusinessSupervisor());
        flowInfo.setApproveStatus("0");
        flowInfo.setApproveProcess("0");
        flowInfo.setDataType("0");
        flowInfo.setCreateUsername(LoginHelper.getLoginUser().getNickname());
        flowInfo.setApproveNickname(remoteUserService.getUserInfo(insertObj.getBusinessSupervisor()).getNickname());


        ApproveInfo approveInfo = new ApproveInfo();
        approveInfo.setStandingBookManageId(id);
        approveInfo.setApproveUsername(LoginHelper.getLoginUser().getUsername());
        approveInfo.setApproveStatus("0");
        approveInfo.setApproveProcess("0");
        approveInfo.setIsAgree("0");
        approveInfo.setDataType("1");
        approveInfo.setCreateUsername(LoginHelper.getLoginUser().getNickname());
        approveInfo.setApproveNickname(LoginHelper.getLoginUser().getNickname());


        int flowRow=approveInfoMapper.insert(flowInfo);
        int approveInfoRow=approveInfoMapper.insert(approveInfo);

        List<StandingFileReq> projectFileList = standingBookManageReq.getStandingFile();
        if (projectFileList != null) {
            for (StandingFileReq pro : projectFileList) {
                pro.setProjectId(insertObj.getId());
                ProjectFile file = new ProjectFile();
                BeanUtil.copyProperties(pro, file);
                projectFileMapper.insert(file);
            }
        }
        return falg;
    }

    @Override
    public ResponsePage<StandingBookManageVo> selectPageList(StandingBookManageReq standingBookManageReq, PageQuery pageQuery) {
//        Page<StandingBookManageVo> page = standingBookManageMapper.selectPageList(pageQuery.build(), standingBookManageReq);
        Page<StandingBookManage> page = standingBookManageMapper.selectPage(pageQuery.build(), buildQuery(standingBookManageReq).orderByDesc(StandingBookManage::getCreateTime));


        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    /**
     * 构造查询条件
     *
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<StandingBookManage> buildQuery(StandingBookManageReq req) {
        return new LambdaQueryWrapper<StandingBookManage>()
                .like(StrUtil.isNotBlank(req.getCompanyName()), StandingBookManage::getCompanyName, req.getCompanyName())
                .like(StrUtil.isNotBlank(req.getEngineeringProject()), StandingBookManage::getEngineeringProject, req.getEngineeringProject())
                .eq(StrUtil.isNotBlank(req.getMineCode()), StandingBookManage::getMineCode, req.getMineCode())
                .eq(StrUtil.isNotBlank(req.getReportingLevel()), StandingBookManage::getReportingLevel, req.getReportingLevel())
                .ge(req.getBeginDateTime() != null, StandingBookManage::getCreateTime, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, StandingBookManage::getCreateTime, req.getEndDateTime());
    }

    @Override
    @Transactional
    public void updateDeleteByIds(List<String> idList) {
       // standingBookManageMapper.deleteBatchIds(idList);
       // standingFileMapper.deleteBatchIds(idList);
        standingBookManageMapper.updateDeleteByIds(idList);
        standingBookManageMapper.updateDeleteBudgetByIds(idList);
    }

    @Override
    @Transactional
    public void updateById(StandingBookManageReq standingBookManageReq) {
        StandingBookManage updateObj = new StandingBookManage();
        BeanUtil.copyProperties(standingBookManageReq, updateObj);
        int updateCount = standingBookManageMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        projectFileMapper.updateDeleteById(updateObj.getId());
        List<StandingFileReq> projectFileList = standingBookManageReq.getStandingFile();
        if (projectFileList != null) {
            for (StandingFileReq pro : projectFileList) {
                pro.setProjectId(updateObj.getId());
                ProjectFile file = new ProjectFile();
                BeanUtil.copyProperties(pro, file);
                file.setProjectFileId(null);
                projectFileMapper.insert(file);
            }
        }
    }

    @Override
    public StandingBookManageVo selectDetailById(String id) {
        StandingBookManage standingBookManage = standingBookManageMapper.selectById(id);
        if (standingBookManage == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        StandingBookManageVo standingBookManageVo = new StandingBookManageVo();
        BeanUtil.copyProperties(standingBookManage, standingBookManageVo);
        List<ProjectFile> projectFiles = projectFileMapper.selectFileById(id);
        standingBookManageVo.setStandingFile(projectFiles);
        return standingBookManageVo;
    }

   /* @Override
    public List<StandingBookManageVo> getProjectList(StandingBookManageReq standingBookManageReq) {
        return projectFileMapper.getProjectList();
    }

    @Override
    public List<LegalPerson> getLegalPerson(StandingBookManageReq standingBookManageReq) {
        return projectFileMapper.getLegalPerson();
    }


    @Override
    public List<SupplierInfoVo> getSupplierInfo(StandingBookManageReq standingBookManageReq) {
        return projectFileMapper.getSupplierInfo();
    }*/
}




